package com.itheima.web.controller;

import com.github.pagehelper.PageInfo;
import com.itheima.domain.Order;
import com.itheima.domain.Permission;
import com.itheima.utils.BeanUtil;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/permission")
public class PermissionServlet extends BaseServlet {
    public void list(HttpServletRequest request, HttpServletResponse response) {
        try {
            int pageNum = 1;
            int pageSize = 5;
            if (StringUtils.isNotBlank(request.getParameter("pageNum"))) {
                pageNum = Integer.parseInt(request.getParameter("pageNum"));
            }
            if (StringUtils.isNotBlank(request.getParameter("pageSize"))) {
                pageSize = Integer.parseInt(request.getParameter("pageSize"));
            }
            PageInfo<Permission> all = permissionService.findAll(pageNum, pageSize);
            request.setAttribute("page", all);
            request.getRequestDispatcher("/WEB-INF/pages/permission/permission-list.jsp").forward(request, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void toAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Permission> all = permissionService.findAll();
        request.setAttribute("permissionList",all);
        request.getRequestDispatcher("/WEB-INF/pages/permission/permission-add.jsp").forward(request, response);
    }
    public void save(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Permission permission = BeanUtil.fillBean(request, Permission.class);
        permissionService.save(permission);
        response.sendRedirect(request.getContextPath()+"/permission?operation=list");
    }

    public void toEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        Permission permission = permissionService.findById(id);
        request.setAttribute("permission",permission);
        List<Permission> permissions = permissionService.findAll();
        request.setAttribute("permissionList",permissions);
        request.getRequestDispatcher("/WEB-INF/pages/permission/permission-show.jsp").forward(request, response);
    }

    public void edit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        Permission permission = BeanUtil.fillBean(request, Permission.class);
        permission.setId(id);
        permissionService.update(permission);
        response.sendRedirect(request.getContextPath()+"/permission?operation=list");
    }

    public void deletes(HttpServletRequest request, HttpServletResponse response){
        String idstr = request.getParameter("ids");
        String[] idArr = idstr.split(",");
        for (int i = 0; i < idArr.length; i++) {
            permissionService.delete(idArr[i]);
        }
        list(request, response);
    }

    public void delete(HttpServletRequest request, HttpServletResponse response){
        String id = request.getParameter("id");
        permissionService.delete(id);
        list(request, response);
    }
}
